﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld.RobotFights
{
    public abstract class Robot
    {
        public int x;
        public int y;

        private bool _hasBeenPrinted = false;
        private int _lastprintedX;
        private int _lastprintedY;

        public abstract void DoStep(Func<int, int, Robot,int> checkIfCellFree);

        protected string        _name = "#";
        protected ConsoleColor _color = ConsoleColor.White;

        protected void MoveLeft()
        {
            this.x--;
        }

        protected void MoveRight()
        {
            this.x++;
        }

        protected void MoveTop()
        {
            this.y--;
        }

        protected void MoveBottom()
        {
            this.y++;
        }


        public void Print()
        {
            Console.CursorVisible = false;

            if (_hasBeenPrinted)
            {
                //clear previous position
                Console.SetCursorPosition(_lastprintedX, _lastprintedY);
                for (int i = 0; i < _name.Length; i++)
                    Console.Write("0");

            }
            Console.ForegroundColor = _color;
            Console.SetCursorPosition(x, y);
            Console.WriteLine(_name);
            _lastprintedX = x;
            _lastprintedY = y;

            _hasBeenPrinted = true;
        }

        public void SetPosition(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
}
